package cn.hy.batch.processer;


import cn.hy.domain.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.support.CompositeItemProcessor;
import org.springframework.batch.item.support.ListItemReader;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Configuration
@Slf4j
public class ItemProcessorDemo {

    //注入任务对象工厂
    @Resource
    private JobBuilderFactory jobBuilderFactory;

    //任务的执行由Step决定,注入step对象的factory
    @Resource
    private StepBuilderFactory stepBuilderFactory;

    //创建Job对象
    @Bean
    public Job jobDemo05() {
        return jobBuilderFactory.get("jobDemo")
                .start(chunk_step2())
                // .listener(new MyJobListener()) //给job添加监听器
                .build();
    }

    //创建Step对象
    @Bean
    public Step chunk_step2() {
        return stepBuilderFactory.get("chunk_step1")
                .<User, User>chunk(2) //表示每一次处理reader两条,reader/process/write
                .faultTolerant()
                .reader(read()) //读取
                .processor(processor()) //处理
                .processor(myCompositeItemProcessor())
                .writer(write()) //写入
                .build();

    }

    public ItemReader<User> read() { //一组字符串，字符内容包含数字和字母
        List<User> users = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setId(i);
            user.setName("java-" + i);
            user.setOrderId(10000 + i);
            users.add(user);
        }
        return new ListItemReader<User>(users);
    }

    /**
     * 组合的处理器
     *
     * @return
     */
    public CompositeItemProcessor<? super User, ? extends User> myCompositeItemProcessor() {
        CompositeItemProcessor<User, User> compositeItemProcessor = new CompositeItemProcessor<>();
        compositeItemProcessor.setDelegates(Arrays.asList(processor(), processor2()));//委派模式，设置多种处理方式
        return compositeItemProcessor;
    }

    /**
     * 数据处理
     *
     * @return
     */
    public ItemProcessor<? super User, ? extends User> processor() {
        return new ItemProcessor<User, User>() {  //只要偶数id的user
            public User process(User user) throws Exception {
                if (user.getId() % 2 == 0) {
                    return user;
                }
                return null;
            }
        };
    }

    /**
     * 数据处理
     *
     * @return
     */
    public ItemProcessor<? super User, ? extends User> processor2() {
        return new ItemProcessor<User, User>() {  //只要偶数id的user
            public User process(User user) throws Exception {
                user.setName(user.getName() + "9999"); //修改用户名
                return user;
            }
        };
    }


    public ItemWriter<User> write() {
        return new ItemWriter<User>() { //打印处理完的内容
            @Override
            public void write(List<? extends User> list) throws Exception {
                log.info("写入数据:" + list);
                System.out.println(list);
            }
        };
    }
}
